<!--
 * @Author: lvye
 * @Description:
 * @Date: 2023-09-06 15:49:21
 * @LastEditTime: 2023-09-07 18:15:39
 * @FilePath: \vue3-ts\src\components\page-modal\page-modal.vue
-->
<template>
  <div class="page-model">
    <el-dialog
      v-model="dialogVisible"
      title="新建用户"
      width="30%"
      center
      destroy-on-close
    >
      <HyForm v-bind="modalConfig" v-model="formData"></HyForm>
      <slot></slot>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleConfirm"> 确认 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, watch } from "vue"
import HyForm from "@/base-ui/form"
// import { useStore } from "@/store"
export default defineComponent({
  components: {
    HyForm
  },
  props: {
    modalConfig: {
      type: Object,
      default: () => ({})
    },
    defaultInfo: {
      type: Object,
      default: () => ({})
    },
    pageName: {
      type: String,
      require: true
    },
    otherInfo: {
      type: Object,
      default: () => ({})
    }
  },
  setup(props) {
    const dialogVisible = ref(false)
    const formData = ref<any>({})
    // 监听defaultInfo变化，初始化赋值
    watch(
      () => props.defaultInfo,
      (newValue) => {
        for (const item of props.modalConfig.formItems) {
          formData.value[`${item.field}`] = newValue[`${item.field}`]
        }
      }
    )

    // const store = useStore()
    const handleConfirm = () => {
      dialogVisible.value = false
      console.log("确认")
      console.log(formData.value)
      console.log(
        {
          ...formData.value,
          ...props.otherInfo
        },
        "formData.value"
      )
      // if (Object.keys(props.defaultInfo).length) {
      //   // 编辑
      //   // 发送请求
      //   store.dispatch("system/editPageDataAction", {
      //     pageName: props.pageName,
      //     editData: {
      //       ...formData.value,
      //       ...props.otherInfo
      //     },
      //     id: props.defaultInfo.id
      //   })
      // } else {
      //   // 新建
      //   store.dispatch("system/createPageDataAction", {
      //     pageName: props.pageName,
      //     newData: {
      //       ...formData.value,
      //       ...props.otherInfo
      //     }
      //   })
      // }
    }
    return {
      dialogVisible,
      handleConfirm,
      formData
    }
  }
})
</script>

<style lang="less"></style>
